package com.schx.budget.service.impl

import com.schx.budget.mapper.BudgetMapper
import com.schx.budget.pojo.Budget
import com.schx.budget.pojo.BudgetTarget
import com.schx.budget.service.BudgetService
import org.springframework.stereotype.Service
import java.math.BigDecimal
import java.text.SimpleDateFormat
import java.util.*
import java.util.stream.Collectors
import java.util.stream.Collectors.groupingBy
import javax.annotation.Resource
import kotlin.collections.ArrayList
import kotlin.collections.HashMap

@Service
class BudgetServiceImpl: BudgetService {

    @Resource
    lateinit var budgetMapper: BudgetMapper

    override fun getCostList(orgid: String): MutableMap<String, Any?> {
        val cal = Calendar.getInstance()
        val simple = SimpleDateFormat("yyyy-MM")
        var year = cal.get(Calendar.YEAR)
        var thisMonth = simple.format(cal.time)
        cal.add(Calendar.MONTH, -1)
//        var lastMonth = simple.format(cal.time)
        var map = HashMap<String, Any?>()
        //费用累计和当月费用
        var list = budgetMapper.getCostList(year.toString(), thisMonth, orgid)
        //累计费用
        var sumAmount = list.stream().mapToDouble({ item -> item.actualAmount }).sum()
        //本月费用
        var nowMonthCost = list.stream().mapToDouble({ item -> item.nowMonthIncome }).sum()
        var lastIncome = list.stream().mapToDouble({ item -> item.lastIncome }).sum()
        var lastMonthIncome = list.stream().mapToDouble({ item -> item.lastMonthIncome }).sum()
        var budgetAmount = budgetMapper.getCostBudget(orgid)
        var preWage = budgetMapper.getPreWage(thisMonth,orgid)
        var otherCost = budgetMapper.getOtherCost(year.toString(),thisMonth,orgid)
        map.put("monthList", list)
        map.put("sumAmount", sumAmount)
        map.put("lastIncome", lastIncome)
        map.put("lastMonthIncome", lastMonthIncome)
        map.put("budgetAmount", budgetAmount)
        map.put("costNowMonth", nowMonthCost)
        map.put("preWage", preWage)
        map.put("otherCost", otherCost)
        return map
    }

    override fun getCostDetail(type: String,orgid: String): MutableMap<String, Any?> {
        var map = HashMap<String, Any?>()
        val cal = Calendar.getInstance()
        val simple = SimpleDateFormat("yyyy-MM")
        var year = cal.get(Calendar.YEAR)
        var thisMonth = simple.format(cal.time)
        var list = budgetMapper.getCostDetail(year.toString(), thisMonth,type,orgid)
        var actualAmount = list.stream().mapToDouble({ item -> item.actualAmount }).sum()
        var nowMonthAmount = list.stream().mapToDouble({ item -> item.nowMonthIncome }).sum()
        var budgetAmount = list.stream().mapToDouble({ item -> item.budgetAmount }).sum()
        var lastMonthIncome = list.stream().mapToDouble({ item -> item.lastMonthIncome }).sum()
        var lastIncome = list.stream().mapToDouble({ item -> item.lastIncome }).sum()
        map.put("actualAmount",actualAmount)
        map.put("nowMonthAmount",nowMonthAmount)
        map.put("budgetAmount",budgetAmount)
        map.put("lastMonthIncome",lastMonthIncome)
        map.put("lastIncome",lastIncome)
        map.put("detailList",list)
        return map
    }

    override fun getCostDetailByMonth(project: String, orgid: String): List<Budget> {
        val cal = Calendar.getInstance()
        var year = cal.get(Calendar.YEAR)
        var list = budgetMapper.getCostDetailByMonth(year.toString(),project,orgid)
        var str = ""
        var detailList: List<Budget> = arrayListOf()
        list.forEach {
            if (!it.month.equals(str)) {
                str = it.month.toString()
                var info = it
                info.month = it.month.toString()
                var itemList = list.filter { it.month.equals(str) }
                itemList.forEach {
                    when (it.project) {
                        "管理费用" -> info.cost1 = it.actualAmount
                        "销售费用" -> info.cost2 = it.actualAmount
                        "损益类费用" -> info.cost3 = it.actualAmount
                    }
                }
                info.actualAmount = info.cost1 + info.cost2 + info.cost3
                when (it.month) {
                    "01" -> info.month = "一"
                    "02" -> info.month = "二"
                    "03" -> info.month = "三"
                    "04" -> info.month = "四"
                    "05" -> info.month = "五"
                    "06" -> info.month = "六"
                    "07" -> info.month = "七"
                    "08" -> info.month = "八"
                    "09" -> info.month = "九"
                    "00" -> info.month = "十"
                    "11" -> info.month = "十一"
                    "12" -> info.month = "十二"
                }
                detailList = detailList.plus(info)
            }
        }
        return detailList
    }

    override fun getIncomeList(orgid:String): MutableMap<String, Any?> {
        val cal = Calendar.getInstance()
        val simple = SimpleDateFormat("yyyy-MM")
        var year = cal.get(Calendar.YEAR) //年份
        var thisMonth = simple.format(cal.time) //本月时间
        //各科目本年毛利明细
//        var list: List<Budget> = arrayListOf()
//        if(orgid.equals("")){
//            list = budgetMapper.getInAll()
//        }else{
//            list = budgetMapper.getIncomeList(year.toString(), thisMonth, orgid)
//        }
        var list = budgetMapper.getIncomeList(year.toString(), thisMonth, orgid)
        var sumAmount = list.stream().mapToDouble({ item -> item.actualAmount }).sum()
        var nowMonthAmount = list.stream().mapToDouble({ item -> item.nowMonthIncome }).sum()
        var sumBudgetAmount = list.stream().mapToDouble({ item -> item.budgetAmount }).sum()
        var lastIncome = list.stream().mapToDouble({ item -> item.lastIncome }).sum()
        var lastMonthIncome = list.stream().mapToDouble({ item -> item.lastMonthIncome }).sum()

        //政府各科目本年毛利
        var list1 = list.filter { it.type3.equals("政府")}
        var sum1 = list1.stream().mapToDouble({ item -> item.actualAmount }).sum()
        var nowMonthSum1 = list1.stream().mapToDouble({ item -> item.nowMonthIncome }).sum()
        var budgetAmount1 = list1.stream().mapToDouble({ item -> item.budgetAmount }).sum()
        var lastIncomeSum1 = list1.stream().mapToDouble({ item -> item.lastIncome }).sum()
        var lastMonthIncomeSum1 = list1.stream().mapToDouble({ item -> item.lastMonthIncome }).sum()
        //企业各科目本年毛利
        var list2 = list.filter { it.type3.equals("企业")}
        var sum2 = list2.stream().mapToDouble({ item -> item.actualAmount }).sum()
        var nowMonthSum2 = list2.stream().mapToDouble({ item -> item.nowMonthIncome }).sum()
        var budgetAmount2 = list2.stream().mapToDouble({ item -> item.budgetAmount }).sum()
        var lastIncomeSum2 = list2.stream().mapToDouble({ item -> item.lastIncome }).sum()
        var lastMonthIncomeSum2 = list2.stream().mapToDouble({ item -> item.lastMonthIncome }).sum()
        var map = HashMap<String, Any?>()
        //毛利中是否包含服务费
        var serviceNum = budgetMapper.getOldServiceIncome(thisMonth,orgid)
        //服务费
        var service = budgetMapper.getService(year.toString(), thisMonth,orgid)
        //原毛利中包含服务费
        if(serviceNum != null) {
            //毛利中包含服务费，需要单独算的服务费为0
            var serviceList1 = list2.filter { it.project.equals("税控服务费")}
            var serviceList2 = list2.filter { it.project.equals("金盾维护费")}
//            var serviceAmount = serviceList1.stream().mapToDouble({ item -> item.nowMonthIncome }).sum()+serviceList2.stream().mapToDouble({ item -> item.nowMonthIncome }).sum()
            var serviceAmount = service.actualAmount
            map.put("serviceIncome", serviceAmount)
            map.put("list1",list1)
            map.put("list2",list2)
            map.put("sumAmount",sumAmount)
            map.put("list1Sum",sum1)
            map.put("list2Sum",sum2)
            map.put("lastIncome",lastIncome)
            map.put("lastMonthIncome",lastMonthIncome)
            //本月合计、企业合计、政府合计
            map.put("nowMonthAmount", nowMonthAmount)
            map.put("sumBudgetAmount", sumBudgetAmount)
            map.put("nowMonthSum1", nowMonthSum1)
            map.put("nowMonthSum2", nowMonthSum2)
            map.put("budgetAmount1", budgetAmount1)
            map.put("budgetAmount2", budgetAmount2)
            map.put("lastIncomeSum1", lastIncomeSum1)
            map.put("lastMonthIncomeSum1", lastMonthIncomeSum1)
            map.put("lastIncomeSum2", lastIncomeSum2)
            map.put("lastMonthIncomeSum2", lastMonthIncomeSum2)
            map.put("service", service)
        }else {
            //原毛利中不包含服务费, 获取分摊服务费
            val serviceList = budgetMapper.getServiceIncome(thisMonth,orgid).filter { !it.project.equals("其他" )}
            var serviceIncome = serviceList.stream().mapToDouble({ item -> item.actualAmount }).sum()
            //累计收入中加入服务费
            if (serviceIncome != null) {
                list2.plus(serviceList)
            }else{
                serviceIncome = 0.0
            }
            list2.forEach {
                if (it.project.equals("政策性收益")) {
                    it.actualAmount += serviceIncome
                    it.nowMonthIncome += serviceIncome
                }
            }
            map.put("sumAmount", ( sumAmount + serviceIncome))
            map.put("nowMonthAmount", nowMonthAmount + serviceIncome)
            map.put("serviceIncome", serviceIncome)
            //本月收入中加入服务费
            map.put("list1",list1)
            map.put("list2",list2)
//            map.put("sumAmount",sumAmount)
            map.put("list1Sum",sum1)
            map.put("list2Sum",sum2 + serviceIncome)
            map.put("lastIncome",lastIncome)
            map.put("lastMonthIncome",lastMonthIncome)
            //本月合计、企业合计、政府合计

            map.put("sumBudgetAmount", sumBudgetAmount)
            map.put("sumBudgetAmount", sumBudgetAmount)
            map.put("nowMonthSum1", nowMonthSum1)
            map.put("nowMonthSum2", nowMonthSum2 + serviceIncome)
            map.put("budgetAmount1", budgetAmount1)
            map.put("budgetAmount2", budgetAmount2)
            map.put("lastIncomeSum1", lastIncomeSum1)
            map.put("lastMonthIncomeSum1", lastMonthIncomeSum1)
            map.put("lastIncomeSum2", lastIncomeSum2)
            map.put("lastMonthIncomeSum2", lastMonthIncomeSum2)
            map.put("service", service)
        }
        return map
    }

    override fun getIncomeDetail(type: String, orgid:String): MutableMap<String, Any?> {
        var map = HashMap<String, Any?>()
        val cal = Calendar.getInstance()
        val simple = SimpleDateFormat("yyyy-MM")
        var year = cal.get(Calendar.YEAR)
        var thisMonth = simple.format(cal.time)
        var list: List<Budget> = arrayListOf()
//        if(orgid.equals("")){
//            list = budgetMapper.getAllInDetail(type)
//        }else{
//            list = budgetMapper.getIncomeDetail(year.toString(), thisMonth,type,orgid)
//        }
        list = budgetMapper.getIncomeDetail(year.toString(), thisMonth,type,orgid)
        var actualAmount = list.stream().mapToDouble({ item -> item.actualAmount }).sum()
        var nowMonthAmount = list.stream().mapToDouble({ item -> item.nowMonthIncome }).sum()
        var budgetAmount = list.stream().mapToDouble({ item -> item.budgetAmount }).sum()
        var serviceNum = budgetMapper.getOldServiceIncome(thisMonth,orgid)
        var lastIncome = list.stream().mapToDouble({ item -> item.lastIncome }).sum()
        var lastMonthIncome = list.stream().mapToDouble({ item -> item.lastMonthIncome }).sum()
        if(serviceNum == null) {
            val serviceList = budgetMapper.getServiceIncome(thisMonth,orgid).filter { !it.project.equals("其他" )}
            var serviceList1 = serviceList.filter { it.project.equals("税控服务费")}
            var serviceList2 = serviceList.filter { it.project.equals("金盾维护费")}
            var serviceIncome1 = serviceList1.stream().mapToDouble({ item -> item.actualAmount }).sum()
            var serviceIncome2 = serviceList2.stream().mapToDouble({ item -> item.actualAmount }).sum()
            list.forEach {
                if (it.project.equals("税控服务费")) {
                    it.actualAmount += serviceIncome1
                    it.nowMonthIncome += serviceIncome1
                    actualAmount += serviceIncome1
                    nowMonthAmount += serviceIncome1
                }else if (it.project.equals("金盾维护费")) {
                    it.actualAmount += serviceIncome2
                    it.nowMonthIncome += serviceIncome2
                    actualAmount += serviceIncome2
                    nowMonthAmount += serviceIncome2
                }
            }
        }
        map.put("actualAmount",actualAmount)
        map.put("lastIncome",lastIncome)
        map.put("lastMonthIncome",lastMonthIncome)
        map.put("nowMonthAmount",nowMonthAmount)
        map.put("budgetAmount",budgetAmount)
        map.put("detailList",list)
        return map
    }

    override fun getIncomeByPerson(project: String, month: String,orgid:String): List<Budget> {
        val cal = Calendar.getInstance()
        var year = cal.get(Calendar.YEAR)
        var thisMonth = month
        when (month) {
            "一月" -> thisMonth = "01"
            "二月" -> thisMonth = "02"
            "三月" -> thisMonth = "03"
            "四月" -> thisMonth = "04"
            "五月" -> thisMonth = "05"
            "六月" -> thisMonth = "06"
            "七月" -> thisMonth = "07"
            "八月" -> thisMonth = "08"
            "九月" -> thisMonth = "09"
            "十月" -> thisMonth = "00"
            "十一月" -> thisMonth = "11"
            "十二月" -> thisMonth = "12"
        }
        thisMonth = year.toString().plus("-" + thisMonth)
        return budgetMapper.getIncomeByPerson(thisMonth,project,orgid)
    }

    override fun getInDetailByMonth(project: String,orgid:String): List<Budget> {
        val cal = Calendar.getInstance()
        var year = cal.get(Calendar.YEAR)
        val simple = SimpleDateFormat("yyyy-MM")
        var thisMonth = simple.format(cal.time)
        var list = budgetMapper.getInDetailByMonth(year.toString(),project,orgid)
        var weekList: List<Budget>
        if (project.equals("金盾维护费") || project.equals("税控服务费")) {
            var service = Budget()
            service.month = "" + thisMonth.substring(5) + ""
            val serviceList = budgetMapper.getServiceIncome(thisMonth,orgid).filter { !it.project.equals("其他" )}
            var serviceList1 = serviceList.filter { it.project.equals(project)}
            var serviceIncome1 = serviceList1.stream().mapToDouble({ item -> item.actualAmount }).sum()
            var list3 = list.filter { it.month.equals(service.month) }
            var sum = list3.stream().mapToDouble({ item -> item.actualAmount }).sum()
            service.actualAmount = serviceIncome1 - sum
            list = list.filter { !it.month.equals(service.month) }
            list = list.plus(service)
            weekList = budgetMapper.getInServiceByWeek(thisMonth,project,orgid)
        }else{
            weekList = budgetMapper.getInDetailByWeek(thisMonth,project,orgid)
        }
        weekList.forEach {
            it.month = "第".plus(it.month + "周")
        }
        list.forEach {
            if(thisMonth.substring(5).equals(it.month)) {
                it.children = weekList
            }
            when (it.month) {
                "01" -> it.month = "一月"
                "02" -> it.month = "二月"
                "03" -> it.month = "三月"
                "04" -> it.month = "四月"
                "05" -> it.month = "五月"
                "06" -> it.month = "六月"
                "07" -> it.month = "七月"
                "08" -> it.month = "八月"
                "09" -> it.month = "九月"
                "00" -> it.month = "十月"
                "11" -> it.month = "十一月"
                "12" -> it.month = "十二月"
            }
        }
        return list
    }

    override fun getResultDetail(orgid: String, type: String): List<BudgetTarget> {
        var map: List<BudgetTarget> = arrayListOf()
        val cal = Calendar.getInstance()
        var year = cal.get(Calendar.YEAR)
        var list = budgetMapper.getResultDetail(year.toString(), orgid,type)
        var str = ""
        list.forEach {
            if (!str.equals(it.content)){
                str = it.content.toString()
                var listItem = list.filter { it.content.equals(str)}
                var info = BudgetTarget()
                info.content = str
                info.type2 = it.type2
                listItem.forEach {
                    when(it.month){
                        "01" -> info.month1 = it.amount_goal
                        "02" -> info.month2 = it.amount_goal
                        "03" -> info.month3 = it.amount_goal
                        "04" -> info.month4 = it.amount_goal
                        "05" -> info.month5 = it.amount_goal
                        "06" -> info.month6 = it.amount_goal
                        "07" -> info.month7 = it.amount_goal
                        "08" -> info.month8 = it.amount_goal
                        "09" -> info.month9 = it.amount_goal
                        "00" -> info.month10 = it.amount_goal
                        "11" -> info.month11 = it.amount_goal
                        "12" -> info.month12 = it.amount_goal
                    }
                }
                map = map.plus(info)
            }
        }
        return map
    }

    override fun getResultAll(orgid: String): List<BudgetTarget> {
        var map: List<BudgetTarget> = arrayListOf()
        val cal = Calendar.getInstance()
        var year = cal.get(Calendar.YEAR)
        var list = budgetMapper.getResultAll(year.toString(), orgid)
        var str = ""
        list.forEach {
            if (!str.equals(it.type2)){
                str = it.type2.toString()
                var listItem = list.filter { it.type2.equals(str)}
                var info = BudgetTarget()
                info.type2 = str
                listItem.forEach {
                    when(it.month){
                        "01" -> info.month1 = it.amount_goal
                        "02" -> info.month2 = it.amount_goal
                        "03" -> info.month3 = it.amount_goal
                        "04" -> info.month4 = it.amount_goal
                        "05" -> info.month5 = it.amount_goal
                        "06" -> info.month6 = it.amount_goal
                        "07" -> info.month7 = it.amount_goal
                        "08" -> info.month8 = it.amount_goal
                        "09" -> info.month9 = it.amount_goal
                        "00" -> info.month10 = it.amount_goal
                        "11" -> info.month11 = it.amount_goal
                        "12" -> info.month12 = it.amount_goal
                    }
                }
                map = map.plus(info)
            }
        }
        return map
    }
}
